var delay = (function(){
	var timer = 0;
	return function(callback, ms){
	clearTimeout (timer);
	timer = setTimeout(callback, ms);
	};
})();
$(document).ready(function(){

	$("input[name='userLogin']").focus();
	$('#form').validate({  
		rules: {  
			userLogin: { required: true, maxlength: 25},
			userPassword: { required: true, maxlength: 20},
			mod: { required: true,}
		},  
		messages: {  
			userLogin: { required: 'Preencha o campo' },
			userPassword: { required: 'Preencha o campo',maxlength:'Digite no maximo 20 digitos' },
			mod: { required: 'Preencha o campo'}
		},  
		 
		submitHandler: function( form ){  
			var dados = $(form).serialize(); 
			var html = '';
			$.ajax({  
				type: "POST",  
				url: "validalogin.php",  
				data: dados,  
				success: function(data)  
				{  
					if(data === 'true'){
						html = '<p class="isa_success">LOGIN V\u00c1LIDO<br/><img src="images/loading.gif" /></p>';
						$('#out').html(html);
						delay(function(){    
							window.location.href = "index.php";
						}, 2000);
						
						//$('#formulario').load('erro_login.php').delay(8000).fadeIn(400);
					}else{
						
						html = '<p class="isa_error">LOGIN INV\u00c1LIDO</p>';
						$('#out').html(html);
						$("#userLogin").focus();
					}
					
				},
				statusCode: {
					404: function() {
					  alert("Pagina nao encontrada");
					 // window.location.href = "consultas.php";
					}
				}  
			});  
			return false;  
		}  
	}); 
});
